﻿--[[
    @brief   ui logic for tray_tips
    @author  zhjyang
    @version 1.0
    @date    2015/07/27
--]]


tray_tips_view = 
{
    tips_dialog = nil;
    tips_contents;
    
    init = function()
    end;
    
    create = function()
        base_utility.log("[tray_tips_view] create");
        layout_mgr.restore_layout("tray_tips.tpf");
        local uilib = ui_utility.uilib();
        tray_tips_view.tips_dialog = ui_utility.get_ctrl_ptr_by_cid(CID_TRAY_TIPS_DLG);
        if tray_tips_view.tips_dialog ~= NULL then
            tray_tips_view.tips_dialog:Show(FALSE);
        end;
        
        local content_ptr = ui_utility.get_ctrl_ptr_by_cid(CID_TRAY_TIPS_CONTENTS);
        if content_ptr ~= NULL then
            tray_tips_view.tips_contents = tolua.cast(content_ptr, "Tenio::ITStatic");
        end;
    end;
    
    show = function(show_animation)
        base_utility.log("[tray_tips_view] show_tips");
        if tray_tips_view.tips_dialog == nil then
            tray_tips_view.create();
        end;
        base_utility.get_component_adapter("ITray_ui","ierd_tgp.tray_ui.get_Tray_ui_adapter()"):show_tray_icon(tray_tips_model.timeout, show_animation);

        if show_animation then
            tray_manager_model.update_tray_status_time("predownload");
            tray_manager_control.switch_status(TRAY_STATUS.TRAY_PREDOWNLOAD_ANI);
        else
            tray_manager_model.remove_tray_status("predownload");
            tray_manager_control.show_preview_tray_icon();
        end
    end;
    
    show_tips = function(x, y)
        base_utility.log("[tray_tips_view] show_tips");
        if tray_tips_view.tips_dialog == nil then
            tray_tips_view.create();
        end;
        local str = tray_tips_model.game_name..tray_tips_model.download_state;
        local const_str, str_holder = U8toCUIStr(str);
        tray_tips_view.tips_contents:SetText(const_str);
        local rc = tray_tips_view.tips_dialog:GetRectEx(TRUE,TRUE);
        local width = rc.right - rc.left;
        local height = rc.bottom - rc.top;
        rc.left = x - width / 2;
        rc.right = x + width / 2;
        rc.top = y - height;
        rc.bottom = y;
        tray_tips_view.tips_dialog:MoveEx(rc,TRUE);
        tray_tips_view.tips_dialog:Show(TRUE);
        base_utility.get_component_adapter("ITray_ui","ierd_tgp.tray_ui.get_Tray_ui_adapter()"):tips_shown();
    end;
    
    hide = function()
        base_utility.log("[tray_tips_view] hide");
        tray_tips_view.tips_dialog:Show(FALSE);
        base_utility.get_component_adapter("ITray_ui","ierd_tgp.tray_ui.get_Tray_ui_adapter()"):tips_hid();
    end;
    
    on_setup = function()
        local param = {
            type = "tools",
            sub_page = "AUTO_UPDATE"
        };
        local json_str = base_utility.table_to_json(param);
        web_utility.open_link(json_str);
        tray_tips_view.hide();
    end;
};

tray_tips_view.init();